<?php
namespace app\api\controller\v1;
use think\facade\Cookie;
use think\Db;
class User extends Base{
	
	/* 
	 用户登陆； 
	 
	 */
	public function login(){
		$data=input('');
		$mobile=input('mobile');
		if(!preg_match("/^1[34578]\d{9}$/", $mobile)){
			return toError(0,'请输入正确手机号');
		}
		if(empty($data['password'])){
			return toError(0,'请填写密码');
		}
		$user_info=Db::name('user')->where('mobile',$mobile)->find();
		if(empty($user_info)){
			return toError(0,'用户信息不存在');
		}
		if(md5($data['password']) !== $user_info['password']){
			return toError(0,'密码错误');
		}
		$team=0;
		if(!empty($user_info['team_id'])){
			if(Db::name("team")->where('t_id',$user_info['id'])->find()){
				$team=1;
			}
		}
		
		return toSuccess('登陆成功',['user_id'=>$user_info['id'],'identity'=>$user_info['identity'],'staff_type'=>(int)$user_info['staff_type'],'team'=>$team]);
	}
	
	public function register(){
		$data=input('');
		if(empty($data['sms_code'])){
			return toError(0,'请填写验证码');
		}
		$mobile_code=Db::name('mobile_code')->where(['mobile'=>$data['mobile'],'status'=>0])->order('add_time desc')->find();
		if($data['sms_code'] != $mobile_code['code']){
			return toError(0,'验证码不正确');
		}
		if($mobile_code['add_time'] < time()){
			return toError(0,'验证码已过期，请重新发送');
		}
		Db::name('mobile_code')->where('id',$mobile_code['id'])->update(['update_time'=>time(),'status'=>1]);
		$insert=[
			'mobile'		=>$data['mobile'],
			'password'		=>md5($data['password']),
			'username'		=>$data['username'],
			'address'		=>$data['address'],
			'identity'		=>$data['identity'],
			'add_time'		=>time(),
		];
		if(Db::name('user')->where(['mobile'=>$data['mobile']])->find()){
			return toError(0,'当前手机号码已注册');
		}
		
		Db::startTrans();
		try {
		    Db::name('user')->insert($insert);
		    // 提交事务
		    Db::commit();
			return toSuccess('注册成功');
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
			return toError(0,'服务器异常');
		}
		
	}
	
	public function forgot_password(){
		$data=input('');
		if(empty($data['sms_code'])){
			return toError(0,'请填写验证码');
		}
		$mobile_code=Db::name('mobile_code')->where(['mobile'=>$data['mobile'],'status'=>0])->order('add_time desc')->find();
		if($data['sms_code'] != $mobile_code['code']){
			return toError(0,'验证码不正确');
		}
		if($mobile_code['add_time'] < time()){
			return toError(0,'验证码已过期，请重新发送');
		}
		Db::name('mobile_code')->where('id',$mobile_code['id'])->update(['update_time'=>time(),'status'=>1]);
		if(Db::name('user')->where(['mobile'=>$data['mobile']])->update(['update_time'=>time(),'password'=>md5($data['password'])])){
			return toSuccess('修改成功');
		}else{
			return toError(0,'操作失败');
		}
		
	}
	
	
	public function open_sms(){
		$mobile=input('mobile');
		if(!preg_match("/^1[34578]\d{9}$/", $mobile)){
			return toError(0,'请输入正确手机号');
		}
		return SendSms($mobile);
	}
	
	
	public function upload_file(){
			if(empty($_FILES)){
				return toError(0,'上传文件为空');
			}
			$img = file_upload('file','./uploads/');
			if($img['code'] == 200){
				return toSuccess('上传成功',['image_path'=>$img['img_url']]);
			}
			return toError(0,$img['msg']);
	}
	
	
	public function user_info(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
			$id=input('user_id');
			$user_info=Db::name('user')->where('id',$id)->field('id,username,mobile,identity,address,label,budget,score,add_time,audit_status,sex,real_name')->find();
			if(!$user_info){
				return toError(0,'用户信息不存在');
			}
			return toSuccess('查询成功',$user_info);
	}
	
	public function up_user(){
		$data=input('post.');
		if(empty($data['user_id'])){
			return toError(0,'操作错误');
		}
		$data["update_time"]=time();
		$user_id = $data['user_id'];
		unset($data['user_id']);
		if(Db::name("user")->where("id",$user_id)->update($data)){
			return toSuccess('修改成功');
		}else{
			return toError(0,'操作错误');
		}
		
	}
	
	
	
	
	
}